package top.eggcode.system.modules.user.model;

import lombok.Data;
import top.eggcode.common.lang.BeanUnit;
import top.eggcode.plugins.dict.Word;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

/**
 * Title: 基本资料
 * Description: TODO
 * Date: 2021/4/28 9:52
 *
 * @author JiaQi Ding
 * @version 1.0
 */
@Data
public class UserView {

    /**
     * 角色id列表
     */
    private List<String> roleIdList;
    private String id;
    private String email;
    private String username;
    private String password;
    private String nickname;
    private String avatar;
    @Word(dict = "sex")
    private String sex;
    private String mobile;
    private LocalDateTime createdAt;
    private Short status;
    private String remark;
    private String dept;

    public static UserView build(UserBiz entity) {

        UserView view = new UserView();
        BeanUnit.copy(entity, view);
        view.setUsername(entity.getUsername());
        view.setStatus(entity.getStatus());

        List<String> roleIdList = new LinkedList<>();
        entity.getRoleList().stream()
                .map(RoleEntity::getId)
                .forEach(roleIdList::add);
        view.setRoleIdList(roleIdList);
        return view;
    }

    public UserBiz convert() {
        UserBiz entity = new UserBiz();
        BeanUnit.copy(this, entity);
        entity.setUsername(username);
        entity.setPassword(password);
        entity.setNickname(nickname);
        entity.setStatus(status);
        entity.setRoleList(new LinkedList<>());
        this.roleIdList.forEach(id -> {
            RoleEntity role = new RoleEntity();
            role.setId(id);
            entity.getRoleList().add(role);
        });
        return entity;
    }

}
